package com.lightdz.tshirtdz.utils;

import org.apache.http.HttpEntity;
import org.apache.http.client.HttpClient;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.entity.ContentType;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import org.springframework.util.MimeType;

import java.net.URI;

public class HttpClientHelper {
    public static String httpGet(String url){
        try {
            CloseableHttpClient httpClient= HttpClientBuilder.create().build();
            HttpGet httpGet=new HttpGet();
            httpGet.setURI(new URI(url));
            httpGet.setConfig(getRequestConfig());
            httpGet.setHeader("content-type", ContentType.APPLICATION_JSON.getMimeType());
            CloseableHttpResponse execute = httpClient.execute(httpGet);
            HttpEntity entity = execute.getEntity();
            String result = EntityUtils.toString(entity);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            return e.getMessage();
        }
    }

    private static RequestConfig getRequestConfig(){
        RequestConfig config = RequestConfig.custom()
                .setConnectionRequestTimeout(3000)
                .setConnectTimeout(3000)
                .setSocketTimeout(3000).build();
       return config;
    }
}
